package indi.zhifa.study2025.oa.admin.facade.menu.impl;

import indi.zhifa.engine.core.common.utils.DtoEntityUtil;
import indi.zhifa.engine.mpenhance.utils.DbDtoEntityUtil;
import indi.zhifa.study2025.oa.admin.facade.menu.IAppFacade;
import indi.zhifa.study2025.oa.common.util.ConstantUtil;
import indi.zhifa.study2025.oa.dao.po.GenAppEntity;
import indi.zhifa.study2025.oa.dao.po.GenMenuEntity;
import indi.zhifa.study2025.oa.model.enums.EAppStatus;
import indi.zhifa.study2025.oa.model.enums.EMenuType;
import indi.zhifa.study2025.oa.model.expose.menu.dto.AppCreateDto;
import indi.zhifa.study2025.oa.model.expose.menu.dto.AppEditDto;
import indi.zhifa.study2025.oa.model.expose.menu.vo.AppVo;
import indi.zhifa.study2025.oa.service.menu.IAppService;
import indi.zhifa.study2025.oa.service.menu.IMenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@RequiredArgsConstructor
@Component
public class AppFacadeImpl implements IAppFacade {

    final IAppService mAppService;
    final IMenuService mMenuService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public AppVo create(AppCreateDto pCreateDto) {

        if(!StringUtils.hasText(pCreateDto.getVersionCode())){
            pCreateDto.setVersionCode("1.0.0");
        }

        GenAppEntity genAppEntity = DbDtoEntityUtil.createFromDto(pCreateDto, GenAppEntity.class);
        genAppEntity.setStatus(EAppStatus.ACTIVE);



        // 创建一个menu的根节点
        GenMenuEntity rootMenu = new GenMenuEntity();
        rootMenu.createInit();
        rootMenu.setVersionCode(ConstantUtil.CURRENT_VERSION);
        rootMenu.setCode(genAppEntity.getCode());
        rootMenu.setName(genAppEntity.getName()+"根目录");
        rootMenu.setCodeCascade(genAppEntity.getCode());
        rootMenu.setMenuType(EMenuType.ROOT);
        rootMenu.setParentId(-1L);
        rootMenu.setAppId(genAppEntity.getId());
        rootMenu.setAppCode(genAppEntity.getCode());

        genAppEntity.setRootMenuId(rootMenu.getId());

        genAppEntity = mAppService.create(genAppEntity);
        rootMenu = mMenuService.create(rootMenu);

        AppVo rtn = DtoEntityUtil.trans(genAppEntity, AppVo.class);

        return rtn;
    }

    @Override
    public AppVo edit(Long pId, AppEditDto pEditDto) {

        GenAppEntity appEntity = mAppService.edit(pId, pEditDto);
        AppVo appVo = DtoEntityUtil.trans(appEntity, AppVo.class);

        return appVo;
    }

    @Override
    public AppVo setStatus(Long pId, EAppStatus pStatus) {

        Map<String,Object> editMap = new HashMap<String,Object>();
        editMap.put("status", pStatus);
        GenAppEntity appEntity = mAppService.edit(pId, editMap);
        AppVo appVo = DtoEntityUtil.trans(appEntity, AppVo.class);
        return appVo;
    }

    @Override
    public AppVo delete(Long pId) {
        GenAppEntity appEntity = mAppService.delete(pId);
        AppVo appVo = DtoEntityUtil.trans(appEntity, AppVo.class);
        return appVo;
    }

    @Override
    public AppVo info(Long pId) {

        GenAppEntity appEntity = mAppService.check(pId);
        AppVo appVo = DtoEntityUtil.trans(appEntity, AppVo.class);

        return appVo;
    }
}
